#!/usr/bin/env perl

use strict;
use warnings;
use Getopt::Long;

use FindBin;
use lib "$FindBin::Bin/../lib";
use UFL::Workflow::Schema;

=head1 NAME

ufl_workflow_sync_schema.pl - Copy data from one schema to another for UFL::Workflow

=head1 SYNOPSIS

    ./script/ufl_workflow_sync_schema.pl -f workflow_test -t workflow_dwc | db2 -vtd%

=head1 DESCRIPTION

Generated the SQL statements for copying data from one instance of
L<UFL::Workflow::Schema> to another.

=head1 AUTHOR

Daniel Westermann-Clark E<lt>dwc@ufl.eduE<gt>

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

##
## Main script
##

main(@ARGV);
sub main {
    my $from      = 'workflow_test';
    my $to        = 'workflow_dwc';
    my $type      = 'DB2';
    my $separator = '%';
    my $user      = 'dbzwap02';
    my $help      = 0;
    die usage() unless GetOptions(
        'from|f=s'      => \$from,
        'to|t=s'        => \$to,
        'type=s'        => \$type,
        'separator|s=s' => \$separator,
        'user|u=s'      => \$user,
        'help|h'        => \$help,
    );
    print usage() and exit() if $help;

    my $schema = UFL::Workflow::Schema->connect;

    my %options = (add_drop_table => 1, separator => $separator);
    my @statements = $schema->deployment_statements($type, undef, undef, \%options);

    # Break the schema export, creation, and import into three passes
    my (@first_pass_statements, @second_pass_statements, @third_pass_statements);

    if ($type eq 'DB2') {
        push @first_pass_statements, "SET CURRENT SCHEMA $from$separator";
        push @first_pass_statements, $schema->export_statements($separator);
        push @first_pass_statements, "SET CURRENT SCHEMA $to$separator";
    }

    foreach my $statement (@statements) {
        if ($statement =~ /ADD CONSTRAINT/
            or $statement =~ /^GRANT/
            or $statement =~ /^DROP TRIGGER/
            or $statement =~ /^CREATE TRIGGER/) {

            push @third_pass_statements, $statement;
        }
        else {
            # Recreate tables
            push @second_pass_statements, $statement;
        }
    }

    if ($type eq 'DB2') {
        push @second_pass_statements, $schema->import_statements($separator);

        push @third_pass_statements, $schema->grant_statements($user, $separator);
        push @third_pass_statements, $schema->trigger_statements($separator);
        push @third_pass_statements, $schema->sequence_statements($separator);
    }

    print "--\n";
    print "-- Generated by $0 on " . scalar(localtime) . "\n";
    print "--\n";
    print "\n";
    print "--\n";
    print "-- First pass\n";
    print "--\n";
    print join "\n\n", @first_pass_statements;
    print "\n\n";
    print "--\n";
    print "-- Second pass\n";
    print "--\n";
    print join "\n\n", @second_pass_statements;
    print "\n\n";
    print "--\n";
    print "-- Third pass\n";
    print "--\n";
    print join "\n\n", @third_pass_statements;
    print "\n";
}


##
## Subroutines
##

sub usage {
    return <<"END_OF_USAGE";
Usage: $0 [OPTION]...

Available options:
  -f, --from           The schema to sync from
  -t, --to             The schema to sync to
  -T, --type           The database type (e.g. DB2)
  -s, --separator      The SQL statement separator
  -u, --user           The database user to grant access to
  -h, --help           Print this help screen and exit
END_OF_USAGE
}
